package org.action;

import org.dao.UserDao;
import org.dao.imp.UserDaoImp;
import org.pojo.User;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	/**
	 * 新增1个action变量addUser,这个变量必须与后面的addUser.jsp页面中定义的网页变量addUser同名
	 * 否则两个struts无法实现两个变量的映射
	 */
	private static final long serialVersionUID = 1L;
	
	private UserDao userdao;
	private User addUser;
	private User updateUserView;
	private User updateUser;
	private User deleteUser;

	public UserAction (){
		super();
		userdao = new UserDaoImp();
	}
	public String addUser(){
		User newUser = new User();
		newUser.setUsername(addUser.getUsername());
		newUser.setPassword(addUser.getPassword());
		//db db = new Db();
		boolean result = userdao.addUser(newUser);
		if(result) 
				return "success";
		else
			return "error";
	}
	public String updateUserView(){
		int id = updateUserView.getId();
		//DB db = new DB();
		updateUserView = userdao.findUser(id);
		return "success";
	}
	public String updateUser(){
		User existUser = new User();
		existUser.setId(updateUser.getId());
		existUser.setUsername(updateUser.getUsername());
		existUser.setPassword(updateUser.getPassword());
		boolean  result = userdao.updateUser(existUser);
		if(result) 
			return "success";
		else
			return "error";
		
	}
	public String deteleUser(){
		int id = deleteUser.getId();
		User delUser = userdao.findUser(id);
		boolean  result = userdao.updateUser(delUser);
		if(result) 
			return "success";
		else
			return "error";
	}
	
}






















